﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace FSC.HaNamAPApplication
{
    public partial class HNAP_AdminDocTypeEdit : System.Web.UI.UserControl
    {
        private string modeform
        {
            get
            {
                string modeform = "a";
                if (Request.QueryString["m"] != null && Request.QueryString["m"].ToString() != "")
                {
                    modeform = Request.QueryString["m"].ToString();
                }
                return modeform;
            }
        }

        private int docTypeId
        {
            get
            {
                int docTypeId = -1;
                if (Request.QueryString["did"] != null && int.TryParse(Request.QueryString["did"].ToString(), out docTypeId))
                {
                    docTypeId = int.Parse(Request.QueryString["did"].ToString());
                }
                return docTypeId;
            }
        }

        public string CurrentUrlWithoutParam
        {
            get
            {
                return Controler.UrlWithoutParam(Request.RawUrl);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (this.modeform.Equals("u"))
                    LoadData();
            }
        }

        private void LoadData()
        {
            HaNamAPDataDataContext db = new HaNamAPDataDataContext(Controler.HaNamAPConnectionString);
            DocumentType obj = db.DocumentTypes.SingleOrDefault(a => a.DocType_ID == this.docTypeId);
            if (obj != null)
            {
                txtName.Text = obj.Name;
            }
        }

        private void UpdateData()
        {
            HaNamAPDataDataContext db = new HaNamAPDataDataContext(Controler.HaNamAPConnectionString);
            DocumentType obj;

            if (this.modeform.Equals("u"))
                obj = db.DocumentTypes.SingleOrDefault(a => a.DocType_ID == this.docTypeId);
            else
                obj = new DocumentType();

            if (obj == null) return;

            obj.Name = txtName.Text;

            if (this.modeform.Equals("a"))
            {
                db.DocumentTypes.InsertOnSubmit(obj);
            }

            db.SubmitChanges();
            Response.Redirect(CurrentUrlWithoutParam + "?page=AdminDocType");
        }

        protected void lbtUpdate_Click(object sender, EventArgs e)
        {
            UpdateData();
        }
    }
}